package cn.springmvc.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.springmvc.common.datasource.DataSourceEnum;
import cn.springmvc.common.datasource.DynamicDataSourceHolder;
import cn.springmvc.entity.Permission;
import cn.springmvc.entity.Role;
import cn.springmvc.repository.RoleRepository;

/**
 * @author Vincent.wang
 *
 */
@Repository
public class RoleDaoService {

    @Autowired
    protected RoleRepository roleRepository;

    public void addRole(Role role) {
        DynamicDataSourceHolder.setDataSourceKey(DataSourceEnum.MASTER.getKey());
        roleRepository.addRole(role);
    }

    public Role findRoleByCode(String code) {
        DynamicDataSourceHolder.setDataSourceKey(DataSourceEnum.MASTER.getKey());
        return roleRepository.findRoleByCode(code);
    }

    public List<Role> findRoles(String userId) {
        DynamicDataSourceHolder.setDataSourceKey(DataSourceEnum.MASTER.getKey());
        return roleRepository.findRoles(userId);
    }

    public void addRolePermission(Role role, Permission permis) {
        DynamicDataSourceHolder.setDataSourceKey(DataSourceEnum.MASTER.getKey());
        roleRepository.addRolePermission(role, permis);
    }

    public Role findRoleById(String id) {
        DynamicDataSourceHolder.setDataSourceKey(DataSourceEnum.MASTER.getKey());
        return roleRepository.findRoleById(id);
    }

}
